﻿(function($) {
$.fn.phone = function(options){
    var settings = {
		getURL : '/getPhone.action',
		id : 0
    };
    var opts = $.extend({},settings, options);
	return this.each(function(){
		$this = $(this);
		var o = $this.metadata ? $.extend(opts, $this.metadata()) : opts;
		$this.css({cursor:'pointer'});
		addPhoneBtn($this,o);
		addButtonEvent($this,o);
	});
};
function addPhoneBtn($this,o){
	$.ajax({
		url:o.getURL+"?id="+o.id,
		dataType:"json", 
		success: function(json){
			if(json.free=='true'){
				$this.append("免费" + json.message);
			}else{
				$this.append(json.message);
			}
		},  
		error: function(){return null;}  
	});
}
function addButtonEvent($this,o){
	$this.click(function(e){
		e.stopPropagation();
		$.ajax({
			url:o.getURL+"?action=get&id="+o.id,
			dataType:"json", 
			success: function(json){
				if(json.get=='true'){
					var time=new Date().getTime();
					$('#phone').attr('src','/phoneimg?phone='+json.tel+'&time='+time);
					$this.empty();
				}else{
					alert(json.message);
				}
			},  
			error: function(){return null;}  
		});
	});
}
})(jQuery);